﻿/*
 * Created by SharpDevelop.
 * User: XPMUser
 * Date: 10/31/2011
 * Time: 4:05 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.ComponentModel;
using System.IO;

namespace KDG.FoldersSize4
{
	/// <summary>
	/// Description of Scanner.
	/// </summary>
	public class Scanner : BackgroundWorker
	{
		public bool Cancelled { get; private set; }
		public long TotalFiles { get; private set; }
		public long TotalDirectories { get; private set; }
		public Scanner()		
		{
			base.WorkerReportsProgress = true;
			base.WorkerSupportsCancellation = true;
			
			TotalDirectories = TotalFiles = 0;
		}
		
		protected override void OnDoWork(DoWorkEventArgs e)
		{
			//base.OnDoWork(e);
			Cancelled = false;
			ScannerItem ret = null;
			string path = e.Argument as string;
			TotalDirectories = TotalFiles = 0;
			if (path != null) 
			{
				ret = ScanDirectory(new ScannerItem(path, path));
	            ret.CalcPprogressTotal(ret.Length);
			}
			
            e.Result = ret;
		}
		
		private ScannerItem ScanDirectory(ScannerItem Directory)
        {
            if (base.CancellationPending) 
            { 
            	Cancelled = true; 
            	return Directory; 
            }

            DirectoryInfo di = new DirectoryInfo(Directory.FullName);
            try
            {
                FileInfo[] fis = di.GetFiles();
                foreach (FileInfo file in fis)
                {
                    Directory.Childs.Add(
                        new ScannerItem(file.Name, file.FullName) 
                                { IsDirectory = false, Length = file.Length }
                        );
                    Directory.Length += file.Length;
                }
                Directory.FilesCount += fis.Length;
                
                this.TotalDirectories++;
                this.TotalFiles += fis.LongLength;
                
                base.ReportProgress(0, new ScannerReportArg() 
                                    { 
                                    	Name = Directory.FullName,
                                    	TotalDirectories = this.TotalDirectories,
                                    	TotalFiles = TotalFiles
                                    }
                                   );

                DirectoryInfo[] dis = di.GetDirectories();
                foreach (DirectoryInfo dir in dis)
                {
                    ScannerItem si = new ScannerItem(dir.Name, dir.FullName) { IsDirectory = true };
                    Directory.Childs.Add(si);
                    ScanDirectory(si);

                    Directory.Length += si.Length;
                    Directory.FilesCount += si.FilesCount;
                    Directory.FoldersCount += si.FoldersCount;
                }
                Directory.FoldersCount += dis.Length;

                Directory.CalcPprogress();

                Directory.SortChilds();
            }
            //catch (UnauthorizedAccessException) { }
            catch (Exception ex) 
            { 
            	base.ReportProgress(0, ex);
            }
            
            finally { }
            
            

            return Directory;
        }
	}
}
